package com.jhgsys.internal.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.base.entity.BaseNotice;
import com.jhgsys.internal.base.mapper.BaseNoticeMapper;
import com.jhgsys.internal.base.service.IBaseNoticeService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 业务通知 Service实现
 *
 * @author Lujiachen
 * @date 2024-08-12 13:07:19
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseNoticeServiceImpl extends ServiceImpl<BaseNoticeMapper, BaseNotice> implements IBaseNoticeService {

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-12 13:07:19
     * @param request QueryRequest
     * @param baseNotice baseNotice
     * @return IPage<BaseNotice>
    */
    @Override
    public IPage<BaseNotice> findBaseNoticePage(QueryRequest request, BaseNotice baseNotice) {
        LambdaQueryWrapper<BaseNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseNotice::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<BaseNotice> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-12 13:07:19
     * @param baseNotice baseNotice
     * @return List<BaseNotice>
     */
    @Override
    public List<BaseNotice> findBaseNoticeList(BaseNotice baseNotice) {
	    LambdaQueryWrapper<BaseNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseNotice::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author Lujiachen
    * @date 2024-08-12 13:07:19
    * @param id
    * @return BaseNotice
    */
    @Override
    public BaseNotice findBaseNoticeById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author Lujiachen
     * @date 2024-08-12 13:07:19
     * @param baseNotice baseNotice
     */
    @Override
    @Transactional
    public void createBaseNotice(BaseNotice baseNotice) {
        baseNotice.setCreatedTime(new Date());
        this.save(baseNotice);
    }

    /**
     * 修改
     * @author Lujiachen
     * @date 2024-08-12 13:07:19
     * @param baseNotice baseNotice
     */
    @Override
    @Transactional
    public void updateBaseNotice(BaseNotice baseNotice) {
        baseNotice.setUpdatedTime(new Date());
        this.saveOrUpdate(baseNotice);
    }

    /**
     * 逻辑删除
     * @author Lujiachen
     * @date 2024-08-12 13:07:19
     * @param baseNotice baseNotice
     * @param baseNoticeIds baseNoticeIds
     */
    @Override
    @Transactional
    public void deleteBaseNotice(BaseNotice baseNotice,String baseNoticeIds) {
        List<String> list = Arrays.asList(baseNoticeIds.split(StringPool.COMMA));
        this.baseMapper.update(baseNotice,new LambdaQueryWrapper<BaseNotice>().in(BaseNotice::getNoticeId,list));
	}

    /**
    * 物理删除
    * @author Lujiachen
    * @date 2024-08-12 13:07:19
    * @param baseNoticeIds baseNoticeIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseNotice(String baseNoticeIds) {
        List<String> list = Arrays.asList(baseNoticeIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseNotice> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseNotice::getNoticeId,list);
        this.remove(wapper);
    }
}
